package com.zhuiyun.project.api.appuser.mapper;

import com.tencentcloudapi.iot.v20180123.models.AppUser;
import com.zhuiyun.project.api.appuser.entity.User;
import com.zhuiyun.project.api.appuser.model.AppUserModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface AppUserMapper extends BaseMapper<User> {
    /**
     * 通过用户名获取用户信息
     *
     * @return java.util.List<com.zhuiyun.project.api.user.entity.User>
     * @author 温
     * @date 2023/6/5 18:08
     * @parameter [nickName, pageRequest]
     */
    @Select("select * from app_user where user_name like '%${userName}%'")
    List<User> userList(@Param("userName") String userName, @Param("PageRequest") PageRequest pageRequest);


    /**
     * 通过标识实现用户禁言功能
     *
     * @return int
     * @author 温
     * @date 2023/6/12 9:52
     * @parameter [User]
     */
    @Update("update app_user set log_off=#{logOff} where id=#{id}")
    int logOffList(User User);


    /**
     * 彻底删除注销的用户信息
     *
     * @return int
     * @author 温
     * @date 2023/6/15 9:11
     * @parameter [id]
     */
    @Delete("delete from app_user where id=#{id}")
    int userDelete(int id);


    /**
     * 根据前端传来的值进行注销并添加注销理由
     *
     * @return int
     * @author 温
     * @date 2023/6/20 16:34
     * @parameter [id, cancellationReason]
     */
    @Insert("insert into  user_cancellation (cancellation_reason,recovery_status,cancellation_id,cancellation_time)values(#{cancellationReason},1,#{cancellationId},NOW()) ")
    int useradd(@Param("cancellationId") Integer cancellationId, @Param("cancellationReason") String cancellationReason);


    /**
     * 修改注销状态
     *
     * @return int
     * @author 温
     * @date 2023/6/20 16:34
     * @parameter [id]
     */
    @Update("update app_user set prohibition=1 where id=#{cancellationId}")
    int userUpdate(Integer id);


    /**
     * @param toId
     * @Author kdj
     * @Description 增加新消息未读数
     * @Date 2023/7/21 16:23
     * @Return void
     */
    @Update("UPDATE  app_user SET friend_new_num =friend_new_num+1 where  id=#{toId} ")
    void addFriendNewNum(int toId);

    /**
     * @param userId
     * @param num
     * @Author kdj
     * @Description 修改请求好友的未读数
     * @Date 2023/7/21 16:41
     * @Return int
     */
    @Update("UPDATE  app_user SET friend_new_num =#{num} where id=#{userId} ")
    int upFriendNewNum(int userId, int num);

    /**
     * @param appUserId
     * @Author 韩佳轩
     * @Description 从app_user表中根据id查出来发送人头像与名称
     * @Date 2023/8/3 15:59
     * @Return com.zhuiyun.project.api.appuser.model.AppUserModel
     */
    @Select("select * from app_user where id=#{appUserId}")
    AppUserModel queryGroupUser(Integer appUserId);

    /**
     * @param appUserId
     * @Author kdj
     * @Description 根据userID获取user的信息
     * @Date 2023/8/3 15:48
     * @Return com.zhuiyun.project.api.appuser.model.AppUserModel
     */
    @Select("select * from app_user where id=#{appUserId}")
    User getUserInfo(Integer appUserId);

    /**
     * @param doctorId
     * @Author kdj
     * @Description 改变类型  普通用户改为医师
     * @Date 2023/8/5 21:46
     * @Return int
     */
    @Update("UPDATE  app_user SET user_type=1 , update_time=now() where id=#{doctorId} ")
    int userToPhy(Integer doctorId);

    /**
     * @param
     * @Author ys
     * @Description 根据id查询用户类型
     * @Date 2023/8/5 21:46
     * @Return int
     */
    @Select("<script>SELECT user_type " +
            "FROM app_user " +
            "WHERE id = #{id}</script>")
    int selectUserType(int id);

    /**
     * @param name
     * @Author kdj
     * @Description 根据name模糊查好友
     * @Date 2023/8/9 15:32
     * @Return java.util.List<com.zhuiyun.project.api.appuser.entity.User>
     */
    @Select("SELECT " +
            "    o.*, " +
            "    MAX(COALESCE(f.request_status, 4)) AS request_status " +
            "FROM " +
            "    app_user o " +
            "LEFT JOIN ( " +
            "    SELECT " +
            "        CASE " +
            "            WHEN request_status IS NULL OR del_flag = 1 THEN 4 " +
            "            WHEN user_id = #{userId} THEN request_status " +
            "            WHEN friend_id = #{userId} AND request_status = 0 THEN 3 " +
            "            ELSE request_status " +
            "        END AS request_status, " +
            "        CASE " +
            "            WHEN user_id = #{userId} THEN friend_id " +
            "            ELSE user_id " +
            "        END AS friend_user_id " +
            "    FROM " +
            "        add_friend " +
            "    WHERE " +
            "        user_id = #{userId} OR friend_id = #{userId} " +
            ") AS f ON o.id = f.friend_user_id " +
            "WHERE " +
            "    o.user_name LIKE CONCAT('%', #{name}, '%') " +
            "    AND o.del_flag = 0 " +
            "    AND o.user_type = 0 " +
            "    AND o.id <> #{userId} " +
            "GROUP BY " +
            "    o.id")
    List<Map> getFriendByNameLike(@Param("name") String name, @Param("userId") int userId);

    /**
     * @param userId
     * @Author kdj
     * @Description 注销用户
     * @Date 2023/8/14 16:04
     * @Return int
     */
    @Update("update   app_user set prohibition=1,update_time=now() where id=#{userId}")
    int toProhibition(int userId);
    /**
     * @param userId
     * @Author kdj
     * @Description 解封用户
     * @Date 2023/8/14 16:04
     * @Return int
     */
    @Update("update   app_user set prohibition=0,update_time=now() where id=#{userId}")
    int openProhibition(int userId);
    /**
     * @param
     * @Author ys
     * @Description 根据手机号查询用户注销状态
     * @Date 2023/8/5 21:46
     * @Return int
     */
    @Select("<script>SELECT prohibition FROM app_user WHERE user_phone = #{userPhone}</script>")
    int selectUserProhibition(String userPhone);
    @Select("<script>SELECT * FROM app_user WHERE user_phone = #{userPhone}</script>")
    AppUserModel getUserInfoByPhone1(String userPhone);
}
